class Solution2 {
    //两两比较
    public static String longestCommonPrefix(String[] strs) {
        if(strs.length == 1){
            return strs[0];
        }
        //以第一个字符串为初始公共前缀
        String prefix = strs[0];
        for (int i = 0; i < strs.length; i++) {
            prefix = bijiao(prefix, strs[i]);
            if(prefix.isEmpty()){
                break;
            }
        }
        return prefix;
    }

    //先解决两个字符串最长公共前缀
    private static String bijiao(String str1, String str2) {
        StringBuilder stringBuilder = new StringBuilder();
        //用指针
        for (int i = 0; i < Math.min(str1.length(), str2.length()); i++) {
            if (str1.charAt(i) != str2.charAt(i)) {
                break;
            }
            stringBuilder.append(str1.charAt(i));
        }
        return stringBuilder.toString();
    }

     public static void main(String[] args) {
         String[] strs = new String[]{"flower", "flow", "flight"};
         String s = Solution1.longestCommonPrefix(strs);
         System.out.println(s);
     }
}